package Messages;

import java.io.IOException;
import java.lang.reflect.Array;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.math3.stat.Frequency;

import ocsf.server.ConnectionToClient;
import server.SQLConnection;
/**
 * this class implements the getting dates from ui and create histogram
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class MessageGetByDate {

	private ArrayList<Integer> countwnated= new ArrayList<Integer>();
	private ArrayList<Integer> countnoraml= new ArrayList<Integer>();
	private ConnectionToClient client;
	private int midw,avgw,avgn,midn; 
	Frequency freq,freq1;
	/**
	 * constractor of this class 
	 * @param client
	 */
	public MessageGetByDate(ConnectionToClient client) {
		this.client= client;
		
	}
	
	/**
	 * generate report 
	 * @param str
	 * @throws SQLException
	 * @throws IOException
	 */
	public void Generate (String[] str) throws SQLException, IOException{

		SQLConnection sc = new SQLConnection();
		PreparedStatement wanted = sc.getConnection().prepareStatement("SELECT sum(avgwanted) FROM db1.lendreportmonth WHERE date >= '"+str[1]+"' and date <= '"+str[2]+"';");
		PreparedStatement midwanted = sc.getConnection().prepareStatement("SELECT sum(midwanted) FROM db1.lendreportmonth WHERE date >= '"+str[1]+"' and date <= '"+str[2]+"';");
		PreparedStatement avgnormal = sc.getConnection().prepareStatement("SELECT sum(avgnormal) FROM db1.lendreportmonth WHERE date >= '"+str[1]+"' and date <= '"+str[2]+"';");
		PreparedStatement midnormal = sc.getConnection().prepareStatement("SELECT sum(midnormal) FROM db1.lendreportmonth WHERE date >= '"+str[1]+"' and date <= '"+str[2]+"';");
		PreparedStatement freqq = sc.getConnection().prepareStatement("SELECT * FROM db1.lendreportmonth WHERE date >= '"+str[1]+"' and date <= '"+str[2]+"';");
		
		

		sc.getConnection().setAutoCommit(true);
		
		java.sql.ResultSet rs1 =  wanted.executeQuery();
		rs1.next();
		this.avgw = rs1.getInt(1);
		
		java.sql.ResultSet rs2 =  midwanted.executeQuery();
		rs2.next();
		this.midw = rs1.getInt(1);
		
		java.sql.ResultSet rs3 =  avgnormal.executeQuery();
		rs3.next();
		this.avgn = rs3.getInt(1);
		
		java.sql.ResultSet rs4 =  midnormal.executeQuery();
		rs4.next();
		this.midn = rs4.getInt(1);
		
		java.sql.ResultSet rs5=freqq.executeQuery();
		int o=0;
		
		while(rs5.next()){
			
			countwnated.add(rs5.getInt(2));
			countnoraml.add(rs5.getInt(4));
		}
		
		
		
		
		/*
		Double val11;
		Collections.sort(countwnated);
		int max1,min1;
		max1=countwnated.get(o);
		min1=countwnated.get(0);
		Double max,min;
		Double prec22;
		 max=(double)max1;
		 min=(double)min1;
		o=countwnated.size();
		 int j=0;
		Double r=(double) ((max-min)/10);
		double end=min+r*10;
		Map<Double, Double> map9 = new HashMap<Double, Double>();
		if(r==0)
			map9.put(max,1.0);
			else{	
		for(double i=min+r;i<=max;i=r+i){
			int count =0;
			while((countwnated.get(j))<=i){
				count=count+1;
				j=j+1;
				if(j==o)
					break;
			}
			prec22=(double)count/o;
			val11 = (double) i; 
			//val22 = (double) count; 
			map9.put(val11, prec22);
			
		}
		Double val1;
		*/
		
		/*//String s=freq.toString();
		int k = 0;
		Double prec2;
		Double val2;
		Map<Double, Double> map2 = new HashMap<Double, Double>();
		while(k<10000){
			
			prec2= (double) freq.getCount(k);
			if(prec2>0){
				//prec=prec*100;
				val2 = (double) k; 
				map2.put(val2, prec2);
			}
			k++;
		
		}
		
		
		Double val;
		
		int i=1;
		Map<Double, Double> map = new HashMap<Double, Double>();
		Double prec;
		while(i<10000){
			
			prec= freq.getPct(i);
			if(prec>0){
				//prec=prec*100;
				val = (double) i; 
				map.put(val, prec);
			}
			i++;
		
		}
		Double val1;
		int y=1;
		Map<Double, Double> map1 = new HashMap<Double, Double>();
		Double prec1;
		while(y<10000){
			
			prec1= freq1.getPct(y);
			if(prec1>0){
				//prec=prec*100;
				val1 = (double) y; 
				map1.put(val1, prec1);
			}
			y++;
		
		}
		*/
		ArrayList<Object> Arr=new ArrayList<Object>();
		Arr.add("showReport");
		Arr.add(this.avgw);
		Arr.add(this.midw);
		Arr.add(this.avgn);
		Arr.add(this.midn);
		Arr.add(creatfreq(countwnated).get(0));
		Arr.add(creatfreq(countwnated).get(1));
		Arr.add(creatfreq(countnoraml).get(0));
		Arr.add(creatfreq(countnoraml).get(1));
		client.sendToClient(Arr);
	}
	protected Frequency calcFreq(ArrayList<Integer> a){
		Frequency f = new Frequency();
		for (Integer x : a){
			f.addValue(x);
		}
		return f;
	}

	
	
	public ArrayList<Object> creatfreq(ArrayList<Integer> countwnated){
		Map<Double, Double> map = new HashMap<Double, Double>();
		Double val11;
		Collections.sort(countwnated);
		int max1,min1,size,j;
		size=countwnated.size()-1;
		max1=countwnated.get(size);
		min1=countwnated.get(0);
		Double max,min;
		Double prec22;
		 max=(double)max1;
		 min=(double)min1;
		
		j=0;
		Double r=(double) ((max-min)/10);
		
		
		if(r==0)
			map.put(max,1.0);
			else{	
		for(double i=min+r;i<=max;i=r+i){
			int count =0;
			while((countwnated.get(j))<=i){
				count=count+1;
				
				if(j==size)
					break;
				j=j+1;
			}
			prec22=(double)count/size;
			val11 = (double) i; 
			map.put(val11, prec22);
			
		}
		
		
		
		
	}
		ArrayList<Object> Arr=new ArrayList<Object>();
		Arr.add(map);
		Arr.add(r);
		return Arr;
	
	
	}
	
}
